package com.woniu.humanresource.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniu.humanresource.dao.PurchaseListMapper;
import com.woniu.humanresource.entity.Purchase;
import com.woniu.humanresource.entity.PurchaseList;
import com.woniu.humanresource.service.PurchaseService;
import com.woniu.humanresource.dao.PurchaseMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
* @author xiang
* @description 针对表【purchase】的数据库操作Service实现
* @createDate 2022-07-12 19:41:12
*/
@Service
public class PurchaseServiceImpl extends ServiceImpl<PurchaseMapper, Purchase>
    implements PurchaseService{

    @Autowired
    PurchaseMapper purchaseMapper;
    @Autowired
    PurchaseListMapper purchaseListMapper;

    //插入采购表
    @Override
    public int addPurchase(Purchase purchase) {
        //生成一个采购单编号
        SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyyMMddHHmmss");
        String num = simpleDateFormat.format(new Date());

        purchase.setNum("CG"+num);//设置采购单编号
        purchase.setStatus(1);//设置状态为1：待入库

        purchaseMapper.insert(purchase);
        return purchase.getId();//返回插入的采购表ID
    }

    //分页查询采购表
    @Override
    public Page<Purchase> getPurchases(int pageIndex, int pageSize, Purchase purchase) {
        //分页
        Page<Purchase> page=new Page<>(pageIndex,pageSize);//页码、页大小

        //查询条件
        QueryWrapper<Purchase> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq(purchase.getStatus()!=null,"purchase_status",purchase.getStatus())//status状态
                .eq(StringUtils.isNotEmpty(purchase.getApplicationdepartment()),"purchase_applicationdepartment",purchase.getApplicationdepartment())//部门
                .like(StringUtils.isNotEmpty(purchase.getApplicationuser()), "purchase_applicationuser", purchase.getApplicationuser());//申请人

        purchaseMapper.selectPage(page, queryWrapper);//调mybatisplus的方法去查询，如果没有条件的话查询所有分页

        return page;
    }

    //单个作废
    @Override
    public void removePurchaseById(int purchaseId) {
        //------------------------修改采购表状态------------------------
        //创建删除的对象。设置id，根据id修改。设置status，修改status为设置的值。
        Purchase newPurchase=new Purchase();
        newPurchase.setId(purchaseId);//修改条件：采购单id
        newPurchase.setStatus(0);//修改的值：状态改为0

        purchaseMapper.updateById(newPurchase);//调mybatisplus的方法去修改状态

        //------------------------修改采购表对应的采购清单表的状态------------------------
        //修改条件：purchaseId等于传过来的值
        UpdateWrapper<PurchaseList> updateWrapper=new UpdateWrapper<>();
        updateWrapper.eq("purchaselist_pid",purchaseId);
        //修改的值
        PurchaseList newpurchaseList=new PurchaseList();
        newpurchaseList.setStatus(0);

        purchaseListMapper.update(newpurchaseList,updateWrapper);//调mybatisplus的方法去修改状态

    }

    //批量作废
    @Override
    public void removeMultiplePurchasesById(List<Purchase> purchases) {
        for(int i=0;i<purchases.size();i++){
            //------------------------修改采购表状态------------------------
            //创建删除的对象。设置id，根据id修改。设置status，修改status为设置的值。
            Purchase newPurchase=new Purchase();
            newPurchase.setId(purchases.get(i).getId());//修改条件：采购单id
            newPurchase.setStatus(0);//修改的值：状态改为0

            purchaseMapper.updateById(newPurchase);//调mybatisplus的方法去修改状态

            //------------------------修改采购表对应的采购清单表的状态------------------------
            //修改条件：purchaseId等于传过来的值
            UpdateWrapper<PurchaseList> updateWrapper=new UpdateWrapper<>();
            updateWrapper.eq("purchaselist_pid",purchases.get(i).getId());
            //修改的值
            PurchaseList newpurchaseList=new PurchaseList();
            newpurchaseList.setStatus(0);

            purchaseListMapper.update(newpurchaseList,updateWrapper);//调mybatisplus的方法去修改状态
        }
    }

    //根据id查询
    @Override
    public Purchase getPurchaseById(int purchaseid) {

        Purchase purchase = purchaseMapper.selectById(purchaseid);//根据id去查询

        return purchase;
    }
}




